package com.avalon.shop.controller;

import com.avalon.core.model.PageParam;
import com.avalon.core.model.RecordRow;
import com.avalon.shop.model.CategoryProductParam;
import com.avalon.shop.model.SearchProductParam;
import com.avalon.shop.service.ProductCategoryService;
import com.avalon.shop.service.ProductService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    private final ProductCategoryService productCategoryService;
    private final ProductService productService;

    public ProductController(ProductCategoryService productCategoryService, ProductService productService) {
        this.productCategoryService = productCategoryService;
        this.productService = productService;
    }

    @PostMapping("/category/get")
    public List<Map> getProductCategory() {
        return productCategoryService.getProductCategory();
    }

    @PostMapping("/get/one")
    public RecordRow getProductOne(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        return productService.getProductOne(id);
    }

    @PostMapping("/get/first")
    public List<Map> getFirstProduct(@RequestBody PageParam pageParam) {
        return productService.getFirstProduct(pageParam);
    }

    @PostMapping("/category/list")
    public List<Map> getCategoryProduct(@RequestBody CategoryProductParam productParam) {
        return productService.getCategoryProduct(productParam);
    }

    @PostMapping("/get/search")
    public List<Map> getSearchProduct(@RequestBody SearchProductParam searchProductParam) {
        return productService.getSearchProduct(searchProductParam);
    }
}
